package com.yirui.bridge.tcp;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Dujie
 * @create 2021/11/4
 * @since 1.0.0
 */
@Slf4j
public class TcpChannelInitializer extends ChannelInitializer<SocketChannel>{
    TcpChannelInboundHandler clientHandler;
    public TcpChannelInitializer(TcpChannelInboundHandler clientHandler) {
        this.clientHandler = clientHandler;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        log.info("正在连接中...");
        ChannelPipeline pipeline = socketChannel.pipeline();

        //pipeline.addLast(new IdleStateHandler(30, 30, 30));
        //读超时时间设置为10s，0表示不监控
        // pipeline.addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS));
        // pipeline.addLast(new ServerHeartBeat());
        pipeline.addLast(clientHandler);
    }

    public void setClient(NettyTcpClient client) {
        clientHandler.setNettyClient(client);
    }
}
